package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Account;
import com.itheima.domain.Result;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class AccountController {
    @Autowired
    private AccountService accountService;

    @PostMapping("/add")
    public Result addAccount(@RequestBody Account account) {
        accountService.addAccount(account);
        return Result.success(null);
    }

    @GetMapping("/find/{id}")
    public Result findAccountById(@PathVariable String id) {
        Account account = accountService.findAccountById(id);
        return Result.success(account);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteAccount(@PathVariable String id) {
        accountService.deleteAccount(id);
        return Result.success(null);
    }

    @PutMapping("/update")
    public Result updateAccount(@RequestBody Account account) {
        accountService.updateAccount(account);
        return Result.success(null);
    }

    @GetMapping("/page")
    public Result findAccount(@RequestBody Map<String,Integer> params) {
        Integer page = params.get("page");
        Integer pageSize = params.get("pageSize");
        PageInfo account = accountService.findAccount(page, pageSize);
        return Result.success(account);
    }


}
